package tzp.taobao.autosale.model;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import tzp.taobao.autosale.util.PropertiesUtil;
import tzp.taobao.autosale.util.RequestUtil;

import com.taobao.api.model.Trade;

public class TradesTableModel extends AbstractTableModel {

	private Vector<Trade> dataVector = new Vector<Trade>();

	private String[] nameArray = new String[7];

	public TradesTableModel() {
		initTitle();
	}
	
	public TradesTableModel(Vector<Trade> dataVector){
		this.dataVector = dataVector;
		initTitle();
	}
	private void initTitle() {
		
		nameArray[0] = PropertiesUtil
				.getString("tradeTable.columnModel.title0");
		nameArray[1] = PropertiesUtil
				.getString("tradeTable.columnModel.title1");
		nameArray[2] = PropertiesUtil
				.getString("tradeTable.columnModel.title2");
		nameArray[3] = PropertiesUtil
				.getString("tradeTable.columnModel.title3");
		nameArray[4] = PropertiesUtil
				.getString("tradeTable.columnModel.title4");
		nameArray[5] = PropertiesUtil
				.getString("tradeTable.columnModel.title5");
		nameArray[6] = PropertiesUtil
				.getString("tradeTable.columnModel.title6");
	}

	// make the table uneditable
	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public String getColumnName(int column) {
		if (column >= nameArray.length) {
			return "";
		}
		return nameArray[column];
	}

	public int getColumnCount() {
		return nameArray.length;
	}

	public int getRowCount() {
		return this.dataVector.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {

		if (rowIndex >= this.dataVector.size()) {
			return "";
		}
		if (columnIndex == 0) {
			return String.valueOf(rowIndex);
		}
		if (columnIndex == 1) {
			return "";
		}
		if (columnIndex == 2) {
			return RequestUtil.returnTradeStatus(dataVector.get(rowIndex).getStatus());
		}
		if (columnIndex == 3) {
			return dataVector.get(rowIndex).getPrice();
		}
		if (columnIndex == 4) {
			return String.valueOf(dataVector.get(rowIndex).getNum());
		}
		if (columnIndex == 5) {
			return dataVector.get(rowIndex).getTitle();
		}
		if (columnIndex == 6) {
			return dataVector.get(rowIndex).getIid();
		}

		return "";
	}

	public void addTrade(Trade result) {
		this.dataVector.add(result);
		this.fireTableDataChanged();
	}
	
	public void updateTrade(Trade trade){
		for(Trade temp : dataVector){
			if(temp.getTid().equalsIgnoreCase(trade.getTid())){
				dataVector.set(dataVector.indexOf(temp), trade);
				break;
			}
		}
		this.fireTableDataChanged();
	}
	
	public void updateStatus(String tid, String mesg){
		for(Trade temp : dataVector){
			if(temp.getTid().equalsIgnoreCase(tid)){
				temp.setStatus(mesg);
				break;
			}
		}
		this.fireTableDataChanged();
	}

	public Trade getWaittingBuyer(){
		
		for(Trade temp : dataVector){
			if(temp.getStatus().equalsIgnoreCase("WAIT_SELLER_SEND_GOODS")){
				return temp;
			}
		}
		
		return null;
	}
	
	public List<Trade> getDataVector() {
		return this.dataVector;
	}
	
	public void updateTrades(ItemsOnSaleTableModel itemsModel){
		Iterator<Trade> it = dataVector.iterator();
		Trade trade;
		while(it.hasNext()){
			trade = it.next();
			if(!itemsModel.isAutoSale(trade.getIid())){
				it.remove();
			}
		}
		this.fireTableDataChanged();
	}
	
}
